package com.one.ui;

import com.one.component.BackGroundPanel;
import com.one.domain.ResultInfo;
import com.one.net.FailListener;
import com.one.net.PostUtils;
import com.one.net.SuccessListener;
import com.one.utils.JsonUtils;
import com.one.utils.PathUtils;
import com.one.utils.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MainInterface {

    JFrame jf = new JFrame("图书馆");

    final int WIDTH = 500;
    final int HEIGHT = 300;

    // 组装视图
    public void init() throws IOException {
        // 设置窗口相关的属性
        jf.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2, (ScreenUtils.getScreenHeight()-HEIGHT)/2, WIDTH, HEIGHT);
        jf.setResizable(false);
        jf.setIconImage(ImageIO.read(new File("img/logo.png")));

        // 设置窗口内容
        BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File("img/library.jpg")));

        // 组装登录相关元素
        Box vBox = Box.createVerticalBox();
        // 组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名: ");
        JTextField uField = new JTextField(15);
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);

        // 组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密    码: ");
        JTextField pField = new JTextField(15);
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(pField);

        // 组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登录");
        JButton registBtn = new JButton("注册");

        // 登录逻辑
        loginBtn.addActionListener(e -> {
            // 获取用户输入的数据
            String username = uField.getText().trim();
            String password = pField.getText().trim();
            Map<String,String> params = new HashMap<>();
            params.put("username",username);
            params.put("password",password);

            // 访问登录接口
            // 请求发送成功 result => 服务器相应的json字符串
            // 当该请求发送失败后执行此方法
            PostUtils.postWithParams("http://localhost:8080/user/login", params, result -> {
                ResultInfo info = JsonUtils.parseResult(result);
                // 判断info中的flag标志
                if(info.isFlag()) {
                    // 登录成功,跳转到主页面
                    JOptionPane.showMessageDialog(jf,"登陆成功!");
                    new ManagerInterface().init();
                    jf.dispose();
                } else {
                    // 登录失败
                    JOptionPane.showMessageDialog(jf,info.getMessage());
                }
            }, () -> JOptionPane.showMessageDialog(jf,"网络异常,请稍后重试"));
        });

        registBtn.addActionListener(e -> {
            // 跳转到注册页面
            try {
                new RegisterInterface().init();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // 当前界面消失
            jf.dispose();
        });

        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(registBtn);

        vBox.add(Box.createVerticalStrut(50));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(btnBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    // 客户端程序入口
    public static void main(String[] args) throws IOException {
            new MainInterface().init();

    }
}
